Skip to content

Conversation

@Kamii0909
Copy link

@Kamii0909 Kamii0909 commented Feb 26, 2025

More details on Hibernate forum.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


https://hibernate.atlassian.net/browse/HHH-19203

Comment on lines -97 to +100
&& Hibernate.isPropertyInitialized( traversableObject, traversableProperty.getName() );
&& persistenceUnitUtil.isLoaded( traversableObject, traversableProperty.getName() );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hibernate.isPropertyInitialized() is documented to be a synonym for PersistenceUtil.isLoaded(), so this should in principle not have fixed anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, the issue is around uninitialized collections. So what we're trying to do here is add an additional check equivalent to Hibernate.isInitialized(collection). Apparently there's an undocumented difference between Hibernate.isPropertyInitialized() and PersistenceUtil.isLoaded() for that collections. At minimum we should document that properly/.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should maybe also change the documentation, because it's not a synonym.

The PersistenceUnitUtil implementation is aware of the SessionFactory and can hence also check laziness of and initialize managed objects that are not enhanced, whereas the Hibernate class can only deal with bytecode enhanced classes.

This is mostly due to the fact that checking if an attribute is actually initialized, we'd have to be able to access the value of the attribute, which we can't without the SessionFactory, because we don't have a PropertyAccess.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, with all bytecode enhancement settings currently deprecated, I don't see any uses for Hibernate.isInitialized anymore. I think at least a documentation update is in order, and would go as far as deprecating the method. I don't think it's possible to reimplement Hibernate.isPropertyInitialized to align PersistenceUnitUtil.isLoaded without significant side effects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hibernate does not require the use of the bytecode enhancer, and most people don't use it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry I meant Hibernate.isPropertyInitialized not Hibernate.isInitialized. Without bytecode enhancement, the only time it returns unintialized (false) is when the passed in Object entity itself is a HibernateProxy returned by emf.getReferenceById, which is completely covered by Hibernate.isInitialized.

@hibernate-github-bot
Copy link

hibernate-github-bot bot commented Feb 26, 2025

Thanks for your pull request!

This pull request appears to follow the contribution rules.

› This message was automatically generated.

@Kamii0909 Kamii0909 reopened this Mar 31, 2025
@Kamii0909
Copy link
Author

I made adjustments and slight cleaning with the changes from #9806. I noticed formatting of relevant files is kinda off, but I refrained from formatting everything because I want to keep the change set minimum.

PersistenceUnitUtil.isLoaded is a fairly heavy-handed approach. I suppose a better implementation would get the PropertyAccess from EntityPersister instead. I didn't immediately do this because collecting these information upfront would be a lot of memory bloat, since I think for the majority of properties, it will never be lazily initialized without the root object being lazy themself. Collecting them lazily on first use wouldn't have much different with PersistenceUnitUtil.

I am not confident in Hibernate internal that I could detect all possibly lazily initialized properties. Aside from associations, I think we also have lazy basic group, but iirc that requires bytecode enhancement, which is well covered already?

@yrodiere yrodiere requested a review from marko-bekhta October 6, 2025 09:57
@yrodiere
Copy link
Member

yrodiere commented Oct 6, 2025

Hey @marko-bekhta would you mind checking if this makes sense, and checking whether there are any unexpected difference with JPATraversableResolver ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants